package com.lej.app_interact.search

import android.text.Editable
import android.text.TextWatcher
import android.widget.TextView
import androidx.lifecycle.lifecycleScope
import com.alibaba.android.arouter.facade.annotation.Route
import com.lej.app_interact.BR
import com.lej.app_interact.INTERACT_PERSON_ID
import com.lej.app_interact.R
import com.lej.app_interact.databinding.InteractActivityInteractSearchBinding
import com.lej.core.base.BaseActivity
import com.lej.core.base.statusHeight
import com.lej.core.dsl.margin_top_px
import com.lej.core.dsl.setOnItemClickListener
import com.lej.core.router.CRouter
import com.lej.core.router.CRouter.Companion.route
import com.lej.core.widget.drawable.CustomDrawable
import com.lej.dataservice.club.bean.interact.CreatorSearchInfo
import com.qmuiteam.qmui.kotlin.onClick
import com.qmuiteam.qmui.util.QMUIStatusBarHelper
import kotlinx.coroutines.launch


@Route(path = CRouter.interactSearch)
class InteractSearchActivity :
    BaseActivity<InteractActivityInteractSearchBinding, InteractSearchViewModel>() {
    private lateinit var mAdapter: InteractSearchAdapter
    private var firstChange = true
    override fun getLayout(): Int = R.layout.interact_activity_interact_search
    private val emptyList = ArrayList<CreatorSearchInfo>()

    override fun initVariableId(): Int = BR.vm

    override fun initData() {
        super.initData()
        QMUIStatusBarHelper.translucent(this)
        QMUIStatusBarHelper.setStatusBarLightMode(this)
        binding.flTitle.margin_top_px = statusHeight
        binding.etSearchInput.background = (
                CustomDrawable(cornerRadius = 20, solidColor = R.color.themeBackground)
                )
        binding.etSearchInput.setIconResource(R.drawable.ic_vector_search)
        mAdapter = InteractSearchAdapter(R.layout.interact_item_interact_search)
        binding.rv.adapter = mAdapter
        binding.rv.setOnItemClickListener { _, i ->
            CRouter.interactPerson.route(INTERACT_PERSON_ID to mAdapter.data[i].creatorId.toString())
        }
    }

    override fun initViewObservable() {
        super.initViewObservable()
        lifecycleScope.launch { viewModel.getNetRecodeData() }

        binding.tvCancel.onClick { onBackPressed() }

        binding.etSearchInput.textChange { searchData() }

        viewModel.visitList.observe(this) {
            if (it.isNotEmpty()) {
                viewModel.recodeVisible.value = true
                mAdapter.setList(it)
            }
        }
    }


    private fun searchData() {
//        if (firstChange) {
//            viewModel.recodeVisible.value = false
//            mAdapter.setList(emptyList)
//            firstChange = false
//        }
        if (binding.etSearchInput.text.isNullOrEmpty()) {
            mAdapter.setList(emptyList)
            viewModel.emptyVisible.value = false
            if (viewModel.visitList.value?.isNotEmpty() == true) {
                viewModel.recodeVisible.value = true
                mAdapter.setList(viewModel.visitList.value)
            }
        } else {
            viewModel.recodeVisible.value = false
            lifecycleScope.launch {
                val searchList = viewModel.getSearchData()
                if (searchList.isNullOrEmpty()) {
                    mAdapter.setList(emptyList())
                    viewModel.emptyVisible.value = true
                } else {
                    viewModel.emptyVisible.value = false
                    mAdapter.setList(searchList)
                }
            }
        }
    }

    private fun TextView.textChange(block: () -> Unit) {
        addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {}
            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {}
            override fun afterTextChanged(s: Editable?) {
                block()
            }
        })
    }

}